package com.application.lucky.fragment.group;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.application.lucky.Lucky;
import com.application.lucky.R;
import com.application.lucky.activity.GroupActivity;
import com.application.lucky.bean.eventbean.GroupShowEvent;
import com.application.lucky.bean.jsonBean.GroupListbean;
import com.application.lucky.gen.GroupListbeanDao;
import com.application.lucky.manager.GroupDataManager;
import com.application.lucky.request.CallBackEx;
import com.application.lucky.request.RedPRequest;
import com.application.lucky.request.nav.UrlManager;
import com.application.lucky.utils.ConnUtil;
import com.application.mylibrary.base.BaseFragment;
import com.application.mylibrary.utils.EventBusUtils;
import com.application.mylibrary.utils.ISharedPreKey;
import com.application.mylibrary.utils.SharedPreferencesUtil;
import com.application.mylibrary.widget.RefreshView;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qunar.im.ui.util.ProfileUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by zhao.liu on 2014/8/21.
 */
public class FragmentGroupMessage extends BaseFragment {

    private RecyclerView rcl_View;
    private MyAdapter myAdapter;
    private RefreshView refreshView;
    private GroupListbeanDao messageGroupBeanDao;

    @Override
    protected void beforeInit(Bundle arguments) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_group_list;
    }

    @Override
    public void initView(View view) {
        messageGroupBeanDao = Lucky.getApplication().getDaoSession().getGroupListbeanDao();
        rcl_View = ((RecyclerView) view.findViewById(R.id.rcl_view));
        refreshView = ((RefreshView) view.findViewById(R.id.refresh_view));
        rcl_View.setLayoutManager(new LinearLayoutManager(mCOntext));
        myAdapter = new MyAdapter(R.layout.item_group_list);
        rcl_View.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                final GroupListbean item = myAdapter.getItem(position);
                if (item != null) {
//                    item.setDbID(item.getRoomTypeId());
                    if (!SharedPreferencesUtil.getBoolean(Lucky.getApplication(), ISharedPreKey.KEY_OPEN_GROUP + item.getRoomTypeId(), false)) {
                        SharedPreferencesUtil.saveBoolean(Lucky.getApplication(), ISharedPreKey.KEY_OPEN_GROUP + item.getRoomTypeId(), true);
                        Observable observable = Observable.create(new ObservableOnSubscribe() {
                            @Override
                            public void subscribe(ObservableEmitter emitter) {
                                try {
                                    List<GroupListbean> list = messageGroupBeanDao.loadAll();
                                    if (ConnUtil.isEmpty(list) || !list.contains(item)) {
                                        messageGroupBeanDao.insertOrReplace(item);
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        observable.subscribeOn(Schedulers.io()).subscribe();
                        EventBusUtils.post(new GroupShowEvent());
                    }

                    Intent intent = new Intent(mCOntext, GroupActivity.class);
                    intent.putExtra("name", item.getRoomTypeName());
                    intent.putExtra("id", item.getRoomTypeId() + "");
                    startActivityAnim(intent);
                }

            }
        });
        refreshView.setRefreshListener(new RefreshView.RefreshListener() {
            @Override
            public void onRefresh() {
                loadData();
            }
        });
        loadData();

    }

    private void loadData() {
        new RedPRequest().red_room_type_list(new CallBackEx<BaseResponse<List<GroupListbean>>>() {
            @Override
            public void onFailed(int code) {
                if (isFinish()) {
                    return;
                }
                refreshView.setFinish();
            }

            @Override
            public void onSuccess(BaseResponse<List<GroupListbean>> listBaseResponse) {
                if (isFinish()) {
                    return;
                }
                refreshView.setFinish();
                List<GroupListbean> dataInfo = listBaseResponse.dataInfo;
                if (dataInfo != null) {
                    myAdapter.setNewData(dataInfo);
                    GroupDataManager.getInstance().setDataInfo(dataInfo);
                    EventBusUtils.post(new GroupShowEvent());
                } else {
                    myAdapter.setNewData(new ArrayList<GroupListbean>());
                }
            }
        });
    }

    static class MyAdapter extends BaseQuickAdapter<GroupListbean, BaseViewHolder> {


        public MyAdapter(int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(BaseViewHolder helper, GroupListbean item) {
            helper.setText(R.id.tv_group_list_name, item.getRoomTypeName())
                    .setText(R.id.tv_group_list_content, item.getRoomTypeDesc() + "");

            String roomTypePic = item.getRoomTypePic();
            boolean http = roomTypePic.startsWith("http");
//            if (http) {
//                ProfileUtils.displayGravatarRoundedCornerByImageSrcGif(((Activity) mContext), roomTypePic, (ImageView) helper.getView(R.id.iv_group_list_img));
//            } else
            ProfileUtils.displayGravatarRoundedCornerByImageSrc(((Activity) mContext), UrlManager.getHeadPic(roomTypePic), (ImageView) helper.getView(R.id.iv_group_list_img));
        }
    }
}

